package ding

import (
	"github.com/gin-gonic/gin"
	"wonlicsys/database"
	"wonlicsys/service/api/dingService"
	"wonlicsys/utils/common"
)

//创建或更新审批模板
func CreatOrUpdateTemplate(c *gin.Context) {
	reqParam := dingService.ReqCreatOrUpdateTemplateStruct{}
	if err := common.GetParamStruct(c, &reqParam); err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	//数据库
	db, err := database.GetDbWonLicGorm()
	if err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	defer database.CloseDbWonLicGorm(db)
	var rspService dingService.ResCreatOrUpdateTemplateStruct
	if err := rspService.CreateOaTempalte(db, reqParam); err != nil {
		common.JsonResult(c, 200, "success", err.Error())
		return
	}
	common.JsonResult(c, 200, "success", rspService.ProcessCode)
}

//申请实例
func StartProcessInstanceWithOptions(c *gin.Context) {

	//数据库
	db, err := database.GetDbWonLicGorm()
	if err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	defer database.CloseDbWonLicGorm(db)

	//todo 请求直接模拟
	var allFormComponentValues []dingService.FormComponentValues
	first := dingService.FormComponentValues{
		Name:  "TextField-11111",
		Value: "aka",
	}
	allFormComponentValues = append(allFormComponentValues, first)
	second := dingService.FormComponentValues{
		Name:  "TextField-22222",
		Value: "bkb",
	}
	allFormComponentValues = append(allFormComponentValues, second)

	reqParam := dingService.ReqProcessInstances{
		OaTempId:                 1,
		OriginatorUserId:         "195513200030610637",
		ProcessCode:              "PROC-C0880D3E-3B83-4D53-A343-8AFE9895D639",
		ActionType:               "NONE",
		UserIds:                  "195513200030610637",
		StartFormComponentValues: allFormComponentValues,
	}

	//调用
	var resService dingService.ResStartProcessInstances
	if err := resService.StartProcessInstances(db, reqParam); err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	common.JsonResult(c, 200, "success", nil)
}
